package com.kyexpress.vms.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.kyexpress.vms.component.AppSettingsComponent;
import com.kyexpress.vms.component.SynchronizeDictDialog;
import com.kyexpress.vms.enitity.DataCenter;
import com.kyexpress.vms.enitity.Dict;
import com.kyexpress.vms.enitity.MyConfig;
import com.kyexpress.vms.util.MyNotifier;
import com.kyexpress.vms.util.baidu.Fanyi4BaiDu;
import com.kyexpress.vms.util.youdao.Fanyi4YouDao;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class synchronizeDict extends AnAction {
    
    private RestTemplate restTemplate = new RestTemplate();
    
    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        //获取当前编辑器对象
        Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
        //获取选择的数据模型
        SelectionModel selectionModel = editor.getSelectionModel();
        //获取当前选择的文本
        String selectedText = selectionModel.getSelectedText();
        DataCenter.SELECT_TEXT = selectedText;
        MyConfig service = MyConfig.getInstance();
        Project project = anActionEvent.getProject();
        Module[] modules = ModuleManager.getInstance(project).getModules();
        String module_name = MyConfig.getInstance().module_name;
        for (Module module : modules) {
            if (module.getName().equals(module_name)) {
                DataCenter.module=module;
            }
            
        }
        DataCenter.reset();
        try {
            ResponseEntity<Map> forEntity = restTemplate.postForEntity(service.dict_host +DataCenter.DICT_URI + selectedText, null, Map.class);
            HttpStatus statusCode = forEntity.getStatusCode();
            if (statusCode.is2xxSuccessful()) {
                HashMap<String, String> data = (HashMap<String, String>) forEntity.getBody().get("data");
                String dict_select = MyConfig.getInstance().dict_select;
                if (data != null && !data.isEmpty()) {
                    String value;
                    String translate;
                    for (Map.Entry<String, String> entry : data.entrySet()) {
                        value = entry.getValue();
                        Dict dict = new Dict();
                        dict.setDictValue(value);
                        dict.setDictKey(entry.getKey());
                        if (AppSettingsComponent.YOU_DAO.equals(dict_select)) {
                            translate = Fanyi4YouDao.getUpCaseString(value);
                        } else {
                            translate = Fanyi4BaiDu.getUpCaseString(value);
                            TimeUnit.SECONDS.sleep(1);
                        }
                        dict.setFieldName(translate);
                        DataCenter.DICT_LIST.add(dict);
                    }
                }
            }
        } catch (Exception e) {
            MyNotifier.notifyError(project,"报错了:"+e.toString());
        }
        

        SynchronizeDictDialog dialog = new SynchronizeDictDialog(project);
        dialog.show();
        
    }
    
}
